package com.apes.importData.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.esb.processor.connector.http.HttpTransporter;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.MapUtil;
import com.apes.importData.model.ImportModel;
import com.apes.importData.respository.ImportModelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName
 * @Description TODO
 * @Author pengyb
 * @Date 2019/6/25 11:56
 * @Version 1.0
 **/
@Service("importModelService")
public class ImportModelService extends DomainService {
    @Autowired
    private ImportModelRepository importModelRepository;

    private static final String APP_KEY = "00001";

    private static final String METHOD = "api.importModel.findByDataModel";

    private static final String VERSION = "1.0";

    private static final String CONTENT_TYPE = "application/x-www-form-urlencoded";

    /**
     * 保存/更新
     *
     * @param request
     * @return
     */
    public ImportModel save(SimpleRequest request) {
        ImportModel importModel = request.getO(ImportModel.class);
        return importModelRepository.saveAndFlush(importModel);
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        ImportModel importModel = request.getO(ImportModel.class);
        importModelRepository.delete(importModel.getId());
        return "删除成功";
    }

    /**
     * 查询对照
     *
     * @param request
     * @return
     */
    public List findByDataModel(SimpleRequest request) {

        //获取实体的领域类及其repsitory
        Class domainClass = JpaUtil.getDomainClass(request.get("businessType").toString());
        CustomRepository repository = JpaUtil.getRepository(domainClass.getName());

        List list = repository.findAll();
        return list;
    }

    /**
     * 执行
     *
     * @param request
     * @return
     */
    public String execute(SimpleRequest request) {
        ImportModel importModel = request.getO(ImportModel.class);

        //headers
        Map data = new HashMap();
        data.put("pdata", request);
        data.put("appKey", APP_KEY);
        data.put("method", METHOD);
        data.put("version", VERSION);
        data.put("body", "{\"businessType\":\"" + importModel.getBusinessType().getId() + "\"}");
        Map headers = new HashMap();
        headers.put("Content-Type", CONTENT_TYPE);


        //调用Http传输器类
        HttpTransporter transporter = new HttpTransporter();
        transporter.setUrl(importModel.getProductionEnvironment().getCode());
        // transporter.setUrl("http://localhost:6060/router");
        transporter.setHeaders(headers);

        SimpleRequest rqst = new SimpleRequest();
        rqst.setData(data);

        //result
        JSONObject jsonObject = JSONObject.parseObject((String) transporter.process(rqst));
        List<Object> ls = (List) jsonObject.get("body");

        /**
         * 获取实体的领域类
         */
        Class domainClass = JpaUtil.getDomainClass(importModel.getBusinessType().getId());

        /**
         * 获取领域对象（全路径类名）的Repository
         */
        CustomRepository repository = JpaUtil.getRepository(domainClass.getName());

        //执行
        ls.forEach(x -> {
            repository.saveAndFlush(MapUtil.getO(x, domainClass));
        });
        return "成功";
    }


}
